home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4399 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: news.ios.com!usenet
  2. From: John Leonard <leonardj@tribeca.ios.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: aborting a constructor
  5. Date: Mon, 29 Jan 1996 23:50:51 -0500
  6. Organization: 12th of Nov, Inc
  7. Message-ID: <310DA3AB.6E32@tribeca.ios.com>
  8. NNTP-Posting-Host: ppp-57.ts-7.hck.idt.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b4 (Win95; I)
  13.  
  14. In an earlier post, I asked how you use 'new' and a constructor in order to deal with the 
  15. following situation:
  16.      You have a class, let's call it WindowClass, which, when it is instantiated, does the 
  17. following:
  18.      1: It opens the file "filename", and gets its size.
  19.      2: It allocates memory to hold "filename".
  20.      3: It loads "filename" into that memory.
  21.      4: It opens a window to display "filename".
  22.      Now let's suppose that after it performs steps 1 thru 3 it cannot open the window, what does it 
  23. do? Furthermore, how do you code for this kind of situation in C++ in general? My first thought was 
  24. that the constructor should de-allocate all of the resources that it had received so far. Then it 
  25. should throw an exception. The question that I had was: what's going to happen to the memory that 
  26. was allocated for the object's data members? I nother words: what happens to an object if its 
  27. constructor aborts? Is it automatically deleted? I still haven't really gotten to the bottom of that 
  28. question but I want to pose a possible solution to this dilemma and see if it works. 
  29.      Instead of the course of action described above, let's say that you have the constructor 
  30. execute the following statements:
  31.  
  32.      delete this;
  33.      throw(error);
  34.  
  35. and you have the destructor of the class de-allocate the objects. I'd like to know if this would 
  36. work.
  37.      John
  38.